package expressions.slogo;

import parsing.Token;
import expressions.IContext;
import expressions.Prefix;

/**
 * Returns one of two possibilities depending on whether a condition is true or
 * false.
 */
@Token(regex = "ifelse")
public class IfElse extends Prefix<Double>
{

    public IfElse()
    {
        super(3);
    }

    @Override
    public Double evaluate(IContext<Double> context)
    {
        Double condition = children.get(0).evaluate(context);

        if (BooleanConstant.toBoolean(condition))
        {
            children.get(1).evaluate(context);
        }
        else
        {
            children.get(2).evaluate(context);
        }

        return condition;
    }

}
